import axios from "axios";
import {
  baseURL
} from "@/config";
class HttpRequest {
  /**
   * @description 构造函数,如果没有传参默认使用baseURL
   * @param {String} baseUrl
   */
  constructor(baseUrl = baseURL) {
    this.baseUrl = baseUrl;
  }

  /**
   *  @description 获取内部的配置
   */
  getInSlideConfig() {
    const config = {
      baseUrl: this.baseUrl,
      headers: {
        //
      }
    };
    return config;
  }

  /**
   * @description axiso拦截器功能
   * @param {*} instance 传入创建的axios实例
   */
  interceptors(instance) {
    // -> 请求拦截器
    instance.interceptors.request.use(config => {
      return config;
    }, error => {
      return Promise.reject(error);
    });
    // -> 响应拦截器
    instance.interceptors.response.use(res => {
      return res;
    }, error => {
      return Promise.reject(error);
    });
  }

  /**
   * @description 请求方法
   * @param {Object} options
   */
  request(options) {
    const instance = axios.create();
    options = Object.assign(this.getInSlideConfig(), options);
    this.interceptors(instance);
    return instance(options);
  }
}
export default HttpRequest;
